package com.yidu.qianfeng.track_fu;

import com.yidu.qianfeng.common.bean.company.EmpInfo;
import com.yidu.qianfeng.common.bean.transport.TrackWaybill;
import com.yidu.qianfeng.common.bean.waybill.Waybill;
import com.yidu.qianfeng.common.util.JsonMsgUtil;
import com.yidu.qianfeng.common.util.PrimaryKeyUtil;
import com.yidu.qianfeng.mapper.SubstationMapper;
import com.yidu.qianfeng.mapper.TrackWaybillMapper;
import com.yidu.qianfeng.mapper.WaybillMapper;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: 符鑫俊
 * @Description: 跟踪记录业务，增删改
 * @Date Created in 9:34 2021/4/21
 * @since 1.0
 */
public class TrackFuService {

    //注入跟踪数据映射
    @Resource(name = "trackWaybillMapper")
    private TrackWaybillMapper trackWaybillMapper;

    //注入运单数据映射
    @Resource(name = "waybillMapper")
    private WaybillMapper waybillMapper;

    //注入分站实体映射
    @Resource(name = "substationMapper")
    private SubstationMapper substationMapper;


    /**
     *  生成跟踪记录1，收件
     * @param waybill 运单实体
     * @param loginEmpInfo 当前登录用户
     * @return 生成运单实体号
     */
    public String generateTrackAccept(Waybill waybill, EmpInfo loginEmpInfo) {
        //跟踪记录
        TrackWaybill trackWaybill = null;
        try {
            trackWaybill = generateTrackWaybillEntity(waybill,loginEmpInfo);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        //跟踪类型
        trackWaybill.setTrackType(1);
        //1:发站库存，收件    新增运单后
        trackWaybill.setTrackStatus("【"+substationMapper.selectById(waybill.getWaybillStartingSubst()).getSubstName()+"】"+"已受理运单。");

        //新增跟踪记录
        trackWaybillMapper.insert(trackWaybill);

        return trackWaybill.getTrackId();
    }

    /**
     *  根据运单生成跟踪记录实体
     * @param waybill 运单实体 ！必须带托运单号
     * @return 生成的实体，包含录入站点、主键、托运单号、跟踪日期
     */
    private TrackWaybill generateTrackWaybillEntity(Waybill waybill,EmpInfo empInfo) throws Exception {
        if (waybill.getWaybillConsignNum() == null || waybill.getWaybillConsignNum() == ""){
            throw new Exception("ERROR: 运单托运单号不能为空!---");
        }
        TrackWaybill trackWaybill = new TrackWaybill();

        //跟踪记录录入站点
        trackWaybill.setTrackRecordSubst(empInfo.getEmpSubstId());
        //生成跟踪主键
        trackWaybill.setTrackId(PrimaryKeyUtil.generateTrackKey(waybill.getWaybillConsignNum()));
        //跟踪表记录的托运单号
        trackWaybill.setTrackConsignNum(waybill.getWaybillConsignNum());
        //生成跟踪日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        trackWaybill.setTrackDate(formatter.format(new Date()));

        return trackWaybill;
    }


    /**
     * 删除运单方法
     * @param trackId 运单id
     * @return 删除结果
     */
    public String deleteTrack(String trackId) {
        //调用跟踪数据访问层方法，删除运单
        int delete = trackWaybillMapper.deleteById(trackId);
        //json包装信息
        return JsonMsgUtil.createJsonMessage(delete, "删除跟踪记录成功", "删除跟踪记录，请稍后再试。");
    }

    /**
     * 修改运单
     * @param track 跟踪实体
     * @return 修改结果
     */
    public String updateTrackById(TrackWaybill track) {
        //调用跟踪数据访问层方法，修改运单
        int update = trackWaybillMapper.updateById(track);
        //json包装信息
        return JsonMsgUtil.createJsonMessage(update, "修改记录成功", "修改记录失败,请稍后再试");

    }

    /**
     * 新增跟踪记录
     * @param trackWaybill 跟踪实体
     * @return 新增结果
     */
    public String addTrack(TrackWaybill trackWaybill) {
        //生成并保存运单跟踪记录的主键
        String trackKey = PrimaryKeyUtil.generateTrackKey(trackWaybill.getTrackConsignNum());
        trackWaybill.setTrackId(trackKey);

        //调用跟踪数据访问层方法,新增运单
        int insert = trackWaybillMapper.insert(trackWaybill);
        //json包装信息
        return JsonMsgUtil.createJsonMessage(insert, "新增跟踪记录成功", "新增跟踪记录失败");
    }

}
